package com.gm.wholesale.cacheData;

import com.gm.commonlib.util.PrefsUtils.PrefUtils;
import com.gm.commonlib.util.logUtils.LogUtil;
import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.wholesale.constant.Constant;

import java.util.Calendar;

public class PatchCache {

    private static final String FILE_CACHE_DATA = "file_patch_data";
    private static final String KEY = "load_size";

    public static void saveLoadSize() {
        PrefUtils.setInt(FILE_CACHE_DATA, getKey(), getLoadSize() + 1);
    }

    public static int getLoadSize() {
        return PrefUtils.getInt(FILE_CACHE_DATA, getKey(), 0);
    }

    private static String getKey() {
        String todayTime = DateTimeUtils.formatDateTime(System.currentTimeMillis(), DateTimeUtils.YMD);
        return todayTime + KEY;
    }

    public static boolean isOutMaxLoadSize() {
        if (getLoadSize() >= Constant.LOAD_PATCH_SIZE) {
            return true;
        }
        return false;
    }

    public static void clearnOldCacheData(){
        Calendar today = Calendar.getInstance();
        // 将今天的日期设置为前一天
        Calendar yesterday = Calendar.getInstance();
        yesterday.set(Calendar.DAY_OF_YEAR, today.get(Calendar.DAY_OF_YEAR) - 1);
        // 获取前一天的日期
        int year = yesterday.get(Calendar.YEAR);
        int month = yesterday.get(Calendar.MONTH) + 1;
        int day = yesterday.get(Calendar.DAY_OF_MONTH);
        String date = year + "-" + month + "-" + day;
        LogUtil.d("clearnOldDataclearnOldData: "+date+KEY);
        PrefUtils.remove(FILE_CACHE_DATA,date+KEY);
    }
}
